package com.esunny.resourcevisualization.service.impl;

import com.esunny.resourcevisualization.domain.entity.EnumItem;
import com.esunny.resourcevisualization.domain.entity.ResourceTypeEntityData;
import com.esunny.resourcevisualization.domain.entity.ResourceTypeField;
import com.esunny.resourcevisualization.domain.response.ReturnStructure;
import com.esunny.resourcevisualization.manage.EnumItemManage;
import com.esunny.resourcevisualization.manage.ResourceTypeEntityDataManage;
import com.esunny.resourcevisualization.manage.ResourceTypeFieldManage;
import com.esunny.resourcevisualization.service.ResourceTypeEntityDataService;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @version 1.0.0.0
 * @ClassName ResourceTypeEntityDataServiceImpl
 * @Description 资源类别实体字段值服务类
 * @Author baochencong
 * @Date 2020/9/28 15:54
 */
@Service
public class ResourceTypeEntityDataServiceImpl implements ResourceTypeEntityDataService {

    @Resource
    private ResourceTypeEntityDataManage resourceTypeEntityDataManage;

    @Resource
    private ResourceTypeFieldManage resourceTypeFieldManage;

    @Resource
    private EnumItemManage enumItemManage;

    @Override
    public ReturnStructure addResourceTypeEntityData(@NotNull ResourceTypeEntityData resourceTypeEntityData) {
        int entityId = resourceTypeEntityData.getResourceTypeEntityId();
        String fieldName = resourceTypeEntityData.getResourceTypeFieldName();
        if (isNotExistedResourceTypeEntityDataByEntityIdAndFieldName(entityId, fieldName)) {
            return resourceTypeEntityDataManage.addResourceTypeEntityData(resourceTypeEntityData);
        }
        return ReturnStructure.ERROR("该资源类别实体字段值已存在", null);
    }

    @Override
    public ReturnStructure updateResourceTypeEntityDataByEntityDataId(int entityDataId, String entityDataValue) {
        if (isExistedResourceTypeEntityDataByEntityDataId(entityDataId)) {
            return resourceTypeEntityDataManage.updateResourceTypeEntityDataByEntityDataId(entityDataId, entityDataValue);
        }
        return ReturnStructure.ERROR("该资源类别实体字段值不存在", null);
    }

    @Override
    public ReturnStructure updateResourceTypeEntityDataByEntityIdAndFieldName(@NotNull ResourceTypeEntityData resourceTypeEntityData) {
        int entityId = resourceTypeEntityData.getResourceTypeEntityId();
        String fieldName = resourceTypeEntityData.getResourceTypeFieldName();
        formatEnumItemValueToKey(resourceTypeEntityData);
        if (!isNotExistedResourceTypeEntityDataByEntityIdAndFieldName(entityId, fieldName)) {
            return resourceTypeEntityDataManage.updateResourceTypeEntityDataByEntityIdAndFieldName(resourceTypeEntityData);
        } else {
            return resourceTypeEntityDataManage.addResourceTypeEntityData(resourceTypeEntityData);
        }
    }

    @Override
    public ResourceTypeEntityData getResourceTypeEntityDataByEntityIdAndFieldName(int entityId, String fieldName) {
        ResourceTypeEntityData resourceTypeEntityDataByEntityIdAndFieldName = resourceTypeEntityDataManage.getResourceTypeEntityDataByEntityIdAndFieldName(entityId, fieldName);
        if (resourceTypeEntityDataByEntityIdAndFieldName != null){
            formatEnumItemKey(resourceTypeEntityDataByEntityIdAndFieldName);
        }
        return resourceTypeEntityDataByEntityIdAndFieldName;
    }

    @Override
    public List<ResourceTypeEntityData> getResourceTypeEntityDataByEntityId(int entityId) {
        List<ResourceTypeEntityData> resourceTypeEntityDataByEntityId = resourceTypeEntityDataManage.getResourceTypeEntityDataByEntityId(entityId);
        formatEnumItemKey(resourceTypeEntityDataByEntityId);
        return resourceTypeEntityDataByEntityId;
    }

    /**
     * 通过资源类别实体id和资源类别实体字段英文名判断资源类别实体字段值是否不存在
     *
     * @param entityId  资源类别实体id
     * @param fieldName 资源类别实体字段英文名
     * @return 是否不存在
     */
    private boolean isNotExistedResourceTypeEntityDataByEntityIdAndFieldName(int entityId, String fieldName) {
        return resourceTypeEntityDataManage.getResourceTypeEntityDataByEntityIdAndFieldName(entityId, fieldName) == null;
    }

    /**
     * 通过资源类别实体字段值id判断资源类别实体字段值是否存在
     *
     * @param entityDataId 资源类别实体字段值id
     * @return 是否存在
     */
    private boolean isExistedResourceTypeEntityDataByEntityDataId(int entityDataId) {
        return resourceTypeEntityDataManage.getResourceTypeEntityDataByEntityDataId(entityDataId) != null;
    }

    /**
     * 格式化枚举项在页面上的显示
     *
     * @param resourceTypeEntityDataByEntityId 需要格式化显示的数据列表
     */
    private void formatEnumItemKey(@NotNull List<ResourceTypeEntityData> resourceTypeEntityDataByEntityId) {
        for (ResourceTypeEntityData item : resourceTypeEntityDataByEntityId) {
            if (item != null){
                formatEnumItemKey(item);
            }
        }
    }

    /**
     * 格式化枚举项在页面上的显示
     *
     * @param entityData 需要格式化显示的数据
     */
    private void formatEnumItemKey(@NotNull ResourceTypeEntityData entityData) {
        ResourceTypeField fieldInfo = resourceTypeFieldManage.getResourceTypeFieldByNameAndFieldName(entityData.getResourceTypeName(), entityData.getResourceTypeFieldName());
        if (fieldInfo != null && fieldInfo.getResourceTypeFieldType().equals("enum")) {
            List<EnumItem> enumItemListByGroupName = enumItemManage.getEnumItemListByGroupName(fieldInfo.getEnumGroupName());
            for (EnumItem enumItem : enumItemListByGroupName) {
                if (entityData.getResourceTypeEntityDataValue().equals(enumItem.getEnumItemKey())) {
                    entityData.setResourceTypeEntityDataValue(enumItem.getEnumItemValue());
                    break;
                }
            }
        }
    }

    /**
     * 格式化枚举项数据插入数据库
     *
     * @param entityData 需要格式化显示的数据
     */
    private void formatEnumItemValueToKey(@NotNull ResourceTypeEntityData entityData) {
        ResourceTypeField fieldInfo = resourceTypeFieldManage.getResourceTypeFieldByNameAndFieldName(entityData.getResourceTypeName(), entityData.getResourceTypeFieldName());
        if (fieldInfo != null && fieldInfo.getResourceTypeFieldType().equals("enum")) {
            List<EnumItem> enumItemListByGroupName = enumItemManage.getEnumItemListByGroupName(fieldInfo.getEnumGroupName());
            for (EnumItem enumItem : enumItemListByGroupName) {
                if (entityData.getResourceTypeEntityDataValue().equals(enumItem.getEnumItemValue())) {
                    entityData.setResourceTypeEntityDataValue(enumItem.getEnumItemKey());
                    break;
                }
            }
        }
    }


}
